/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.core.extraction;

/**
 *
 * @author Latifah Dwi Nurachma
 */
public class AbstractExtractor {
    private static final String ABSTRACT_TITLE = "abstract";
    private static final String INTRODUCTION_TITLE = "introduction";

    public static String extract(String stringInput){
        String abstractString = "";
        
        //cari posisi huruf pertama abstrak dokumen
        String fiftyFirstRowString = getFiftyFirstRowString(stringInput);

        //jika mengandung kata "abstract" pada 50 baris pertama dokumen, ambil abstraknya
        if(isContainAbstractTitle(fiftyFirstRowString)){

            int ljudulPosition = getLastJudulPosition(fiftyFirstRowString);
            System.out.println("JUDUL ="+fiftyFirstRowString.substring(0, ljudulPosition));
            int firstAbstractPosition = getFirstAbstractPosition(fiftyFirstRowString);
            int lastAbstractPosition = getLastAbstractPosition(stringInput, firstAbstractPosition);
            abstractString = stringInput.substring(firstAbstractPosition, lastAbstractPosition+1).trim();
        }
        System.out.println("Abstract:");
        System.out.println(abstractString);
        return abstractString;
    }

    private static int getLastJudulPosition(String stringInput){
        return (stringInput.toLowerCase().indexOf(ABSTRACT_TITLE));
    }
    private static int getFirstAbstractPosition(String stringInput){
        return (stringInput.toLowerCase().indexOf(ABSTRACT_TITLE) + ABSTRACT_TITLE.length() + 1);
    }

    private static String getFiftyFirstRowString(String stringInput) {
        stringInput = stringInput.trim();
        String fiftyFirstRowString = "";
        int iRow = 0;
        int iChar = 0;
        int stringLength = stringInput.length();
        while(iRow<50 && iChar<stringLength){
            char cTemp = stringInput.charAt(iChar);
            if(isRowSeparator(cTemp)){
                iRow++;
            }
            fiftyFirstRowString = fiftyFirstRowString + cTemp;
            iChar++;
        }
        return fiftyFirstRowString;
    }

    private static int getLastAbstractPosition(String stringInput, int firstPos) {
        int pos=firstPos;
        String abstractArea = stringInput.substring(firstPos).trim().toLowerCase();
        System.out.println("stringInput="+stringInput);
        System.out.println("abstractArea="+abstractArea);
        System.out.println("pos="+pos);
        System.out.println("stringInput.length="+stringInput.length());
        int i=0;
        int lastIndex = stringInput.length()-firstPos;
        System.out.println("lastIndex="+lastIndex);
        while(i<500 && pos<lastIndex){
        while(i<500 && pos<stringInput.length()){
            pos = pos + nearestWhitespace(abstractArea, pos);
            i++;
        }
        }

        abstractArea.substring(firstPos, pos);
        
        if(abstractArea.contains(INTRODUCTION_TITLE)){
            pos = abstractArea.indexOf(INTRODUCTION_TITLE);
            abstractArea = abstractArea.substring(0, pos).trim();
            
            pos = abstractArea.length();
            while(abstractArea.charAt(pos-1)=='1' ||
                abstractArea.charAt(pos-1)=='i' ||
                abstractArea.charAt(pos-1)=='a'){
                pos = pos-1;
                pos = abstractArea.substring(0, pos).trim().length();
            }
            while(abstractArea.charAt(pos-1)=='.' &&
                (abstractArea.charAt(pos-2)=='1' ||
                abstractArea.charAt(pos-2)=='i' ||
                abstractArea.charAt(pos-2)=='a')){
                pos=pos-2;
                pos = abstractArea.substring(0, pos).trim().length();
            }
            pos = pos + firstPos;
        }
        return pos;
    }

    private static boolean isContainAbstractTitle(String stringInput) {
        return stringInput.toLowerCase().contains(ABSTRACT_TITLE.toLowerCase());
    }

    private static boolean isRowSeparator(char c) {
        return (c=='\n' || c=='\r');
    }

    private static int nearestWhitespace(String stringInput, int pos) {
        String subString = stringInput.trim().substring(pos).trim();
        int spacePos = subString.indexOf(' ');
        int nPos = subString.indexOf('\n');
        int rPos = subString.indexOf('\r');
        int minPos=1;
        if(spacePos<nPos){
            minPos=spacePos;
        }
        else{
            minPos=nPos;
        }
        if(rPos<minPos){
            minPos=rPos;
        }
        return minPos;
    }
}
